package com.java.forum.c_service;

import com.java.forum.a_entity.CComment;
import com.java.forum.a_entity.PComment;
import com.java.forum.b_mapper.CCommentMapper;
import com.java.forum.b_mapper.PCommentMapper;
import com.java.forum.b_mapper.PostMapper;
import com.java.forum.g_exception.Exceptions;
import com.java.forum.g_exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    private PCommentMapper pCommentMapper;
    private PostMapper postMapper;
    private CCommentMapper cCommentMapper;

    @Autowired
    public void setpCommentMapper(PCommentMapper pCommentMapper) {
        this.pCommentMapper = pCommentMapper;
    }

    @Autowired
    public void setPostMapper(PostMapper postMapper) {
        this.postMapper = postMapper;
    }

    @Autowired
    public void setPostMapper(CCommentMapper cCommentMapper) {
        this.cCommentMapper = cCommentMapper;
    }

    @Override
    public PComment commentPost(PComment pComment) {
        pComment.setCcomments(0);
        pComment.setDate(new Date());
        Integer res = pCommentMapper.commentPost(pComment);
        if (res != 1) throw new ServiceException(Exceptions.commentPostException);
        Integer res1 = postMapper.PostComment(pComment.getPid());
        if (res1 != 1) throw new ServiceException(Exceptions.commentPostException);
        return pComment;
    }

    @Override
    public PComment getCommentInfo(Integer cid) {
        PComment pComment = pCommentMapper.getPCommentByCid(cid);
        if (pComment == null) throw new ServiceException(Exceptions.PCommentNotFound);
        return pComment;
    }

    @Override
    public List<PComment> getCommentByPid(Integer pid, Integer startCid) {
        return pCommentMapper.getPCommentByPid(pid, startCid);
    }

    @Override
    public void DeleteComment(PComment pComment) {
        Integer c_pid = pCommentMapper.getPCommentByCid(pComment.getCid()).getPid();
        Integer res = pCommentMapper.DeleteComment(pComment);
        if (res < 1) throw new ServiceException(Exceptions.CommentDeleteException);
        Integer res1 = postMapper.PostDeleteComment(c_pid);
        if (res1 != 1) throw new ServiceException(Exceptions.CommentDeleteException);
    }

    @Override
    public Integer commentComment(CComment cComment) {
        cComment.setDate(new Date());
        Integer res1 = pCommentMapper.PCommentAddCComment(cComment.getCid());
        if (res1 != 1) throw new ServiceException(Exceptions.commentPostException);
        Integer res = cCommentMapper.commentComment(cComment);
        if (res != 1) throw new ServiceException(Exceptions.CCommentException);
        return cComment.getCcid();
    }

    @Override
    public List<CComment> CCommentList(Integer cid, Integer startCcid) {
        return cCommentMapper.getCCommentByCid(cid, startCcid);
    }
}
